import { _decorator, Button, Component, Label, Node, Sprite } from 'cc';
import { PopManager } from 'db://assets/script/manager/PopManager';
import { ConfigKey, PopView } from 'db://assets/script/manager/AppControll';
import { GameData } from 'db://assets/script/system/GameData';
import { DataHelper } from 'db://assets/script/system/DataHelper';
import { SoundManager } from '../../manager/SoundManger';
const { ccclass, property } = _decorator;

@ccclass('LevelChallengeBtn')
export class LevelChallengeBtn extends Component {
    private ndPoint: Node = null
    private cur = 0;
    private interval = 400;
    private isStart = false;
    protected onLoad(): void {
        this.ndPoint = this.node.getChildByName("yellowPoint");
        this.node.on(Node.EventType.TOUCH_END, this.OnClick, this);
        this.InitLevelChallgeBtn();
        this.isStart = true;
    }

    InitLevelChallgeBtn() {
        if (GameData.playerData.Honor >= Number.parseInt(DataHelper.GetConfigByKey(ConfigKey.levelChallengeUnlock).value)) {
            this.node.getComponent(Button).interactable = true;
            this.node.getComponent(Sprite).grayscale = false;
            this.node.getChildByName("unlockText").active = false;
        } else {
            this.node.getComponent(Button).interactable = false;
            this.node.getComponent(Sprite).grayscale = true;
            this.node.getChildByName("unlockText").active = true;
        }

    }

    protected update(dt: number): void {
        if (!this.isStart) return;
        this.cur++;
        if (this.cur % this.interval == 0) {
            if (GameData.GetCurlevelChallengeFreeTimes() > 0 && GameData.playerData.Honor >= Number.parseInt(DataHelper.GetConfigByKey(ConfigKey.levelChallengeUnlock).value)) {
                this.ndPoint.active = true;
            } else {
                this.ndPoint.active = false;
            }
            this.InitLevelChallgeBtn();
        }
    }

    OnClick() {
        if (!this.node.getComponent(Button).interactable) return;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        PopManager.Instance<PopManager>().ShowPop(PopView.levelChallenge)
    }
}


